package com.eastfair.auth.dao;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.auth.dto.RoleDTO;
import com.eastfair.auth.dto.UserAccountDto;
import com.eastfair.auth.entity.Organization;
import com.eastfair.auth.entity.Role;
import com.eastfair.auth.entity.UserAccount;
import com.eastfair.auth.vo.UserAccountVo;
import com.eastfair.boot.mapper.SuperMapper;
import com.mysql.cj.x.protobuf.MysqlxCrud;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * 用户账户信息
 * </p>
 *
 * @author ligang
 * @date 2021-07-13
 */
@Repository
public interface BusinessMapper {

    @Select("<script>"
            +"select u.* from c_user_account u "
            +"${ew.customSqlSegment}"
            +"</script>")
    IPage<UserAccountVo> queryUser(IPage<UserAccountDto> page, @Param(Constants.WRAPPER) Wrapper<UserAccountDto> queryWrapper);

    /**
     * 登录接口查询用户
     * @param userAccount
     * @return
     */
    @InterceptorIgnore(tenantLine = "true")
    @Select("<script>"
            +"select * from c_user_account where is_deleted=0 "
            +"<if test='userAccount.name != null'>"
            +"and (name = #{userAccount.name} or mobile = #{userAccount.name})"
            +"</if>"
            +"<if test='userAccount.mobile != null'>"
            +"and mobile = #{userAccount.mobile}"
            +"</if>"
            +"<if test='userAccount.email != null'>"
            +"and email = #{userAccount.email}"
            +"</if>"

            +"<if test='userAccount.iamUserId != null'>"
            +"and iam_user_id like CONCAT(CONCAT('%',#{userAccount.iamUserId}), '%') "
            +"</if>"

            +"<if test='userAccount.id != null'>"
            +"and id =#{userAccount.id} "
            +"</if>"

            +"</script>")
    List<UserAccount> listUserName(@Param("userAccount")UserAccount userAccount);



    /**
     * 登录接口查询用户
     * @param roleDTO
     * @return
     */
    @InterceptorIgnore(tenantLine = "true")
    @Select("<script>"
            +"select * from c_role where is_deleted=0 "
            +"<if test='roleDTO.sourceRoleId != null'>"
            +"and source_role_id = #{roleDTO.sourceRoleId} "
            +"</if>"
            +"</script>")
    List<Role> listRole(@Param("roleDTO")RoleDTO roleDTO);

    /**
     * 登录接口查询用户
     * @param userAccount
     * @return
     */
//    @InterceptorIgnore(tenantLine = "true")
//    @Select("<script>"
//            +" select * from c_user_account u LEFT JOIN c_user_role r on u.id = r.user_id,c_user_organization o where u.id = o.user_id "
//            +"<if test='userAccount.id != null'>"
//            +"and u.id = #{userAccount.id} "
//            +"</if>"
//            +"</script>")
//    @Update("<script>"
//            +" update c_user_organization o , c_user_role r  set o.user_id = 1869275011695251456 where o.user_id = 1869275011695251456 and o.id = 1869276764041908225 and o.user_id = r.user_id "
//            +"</script>")
    @Delete("<script>"
            +" delete from c_user_organization where user_id =1232132 "
            +"</script>")
    List<UserAccount> test3(@Param("userAccount")UserAccount userAccount);
}
